﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tetris
{
    public partial class FrmMain : Form
    {
        private Game game;

        public FrmMain()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Start按钮点击事件：开始游戏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            game = new Game();
            picBoxGame.Height = Game.blockImageHeight * Game.blocksHeight + 3;
            picBoxGame.Width = Game.blockImageWidth * Game.blocksWidth + 3;
            //重绘游戏区域
            picBoxGame.Invalidate(); 
            timerGame.Enabled = true;
            btnStart.Enabled = false;
        }
        /// <summary>
        /// Pause按钮点击事件：暂停游戏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPause_Click(object sender, EventArgs e)
        {
            if (btnPause.Text == "Pause")
            {
                timerGame.Enabled = false;
                btnPause.Text = "Continue";
            }
            else
            {
                timerGame.Enabled = true;
                btnPause.Text = "Pause";
            }
        }
        /// <summary>
        /// End按钮点击事件：结束游戏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEnd_Click(object sender, EventArgs e)
        {
            Close();
        }
        /// <summary>
        /// 重绘游戏区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void picBoxGame_Paint(object sender, PaintEventArgs e)
        {
            if (game != null)
            {
                game.Drawblocks(e.Graphics);
                game.DrawthisBlock(e.Graphics);
            }
        }
        /// <summary>
        /// 重绘提示区域
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void picBoxHint_Paint(object sender, PaintEventArgs e)
        {
            if (game != null) game.DrawNextBlock(e.Graphics);
        }
        /// <summary>
        /// 计时器事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timerGame_Tick(object sender, EventArgs e)
        {
            if (!game.DownBlock())
            {
                picBoxGame.Invalidate(); 
                picBoxHint.Invalidate(); 
            }
            lblScore.Text = game.score.ToString();
            if (game.GameOver)
            {
                timerGame.Enabled = false;
                MessageBox.Show("游戏结束啦！", "提示");
                btnStart.Enabled = true;
            }
        }
        /// <summary>
        ///  重写ProcessCmdKey方法
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys e)
        {
            if (btnPause.Text == "Continue") return true; 
            if (e == Keys.Up || e == Keys.Down || e == Keys.Space ||
                e == Keys.Left || e == Keys.Right)
            {
                MyKeyPress(this, new KeyPressEventArgs((char)e));
            }
            return true;
        }
        /// <summary>
        /// 在MyKeyPress方法中处理   
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MyKeyPress(object sender, KeyPressEventArgs e)
        {
            switch (e.KeyChar)
            {
                case (char)Keys.Up:
                    game.RotatethisBlock();
                    break;
                case (char)Keys.Down:
                    if (!game.DownBlock())
                        picBoxGame.Invalidate(); 
                    break;
                case (char)Keys.Right:
                    game.MovethisBlock(false);
                    break;
                case (char)Keys.Left:
                    game.MovethisBlock(true);
                    break;
                case (char)Keys.Space:
                    btnPause.PerformClick();
                    break;
            }
            picBoxGame.Invalidate();
            picBoxHint.Invalidate();
        }
        /// <summary>
        /// About Me按钮点击事件：弹出窗口显示我的信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAboutMe_Click(object sender, EventArgs e)
        {
            FrmAboutMe frm = new FrmAboutMe();
            frm.ShowDialog();
        }
    }

}